package com.haiqiu.file.ftp.config;

import com.haiqiu.file.ftp.utils.FtpUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author HaiQiu
 * @since 2022/11/29
 */
@Component
public class FtpClientPool {

    /**
     * ftp连接池
     */
    private GenericObjectPool<FTPClient> pool;

    @Autowired
    private FtpClientFactory ftpClientFactory;


    public FtpClientPool(FtpClientFactory ftpClientFactory) {
        this.ftpClientFactory = ftpClientFactory;
        GenericObjectPoolConfig<FTPClient> poolConfig = new GenericObjectPoolConfig<>();
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTestWhileIdle(true);
        poolConfig.setMinEvictableIdleTimeMillis(60000);
        poolConfig.setSoftMinEvictableIdleTimeMillis(50000);
        poolConfig.setTimeBetweenEvictionRunsMillis(30000);
        //最大连接数
        poolConfig.setMaxTotal(300);
        //最大空闲连接数
        poolConfig.setMaxIdle(30);
        pool = new GenericObjectPool<>(ftpClientFactory, poolConfig);
        FtpUtils.init(pool);
    }
}
